package cc.cc4414.cc03.core.webSocket;

import javax.websocket.server.ServerEndpointConfig.Configurator;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * 使WebSocket中能够注入Bean
 * 
 * @author cc 2018年10月6日
 */
@Configuration
public class WebSocketConfigurator extends Configurator implements ApplicationContextAware {
	private static volatile BeanFactory context;

	@Override
	public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
		return context.getBean(clazz);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		WebSocketConfigurator.context = applicationContext;
	}
}
